﻿using System;
using System.Data;
using Microsoft.Extensions.Configuration;

namespace EC.Entity
{
    using MySql.Data.MySqlClient;

    public class Base:IDisposable
    {
        /// <summary>
        /// mysql连接
        /// </summary>
        public static MySqlConnection connection;

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static readonly string _connectionStrings = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build()["ConnectionStrings:mysqlconnection"];

        /// <summary>
        /// 图片服务器地址
        /// </summary>
        public static readonly string _serverURL = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build()["Server:ImageServer"];

        /// <summary>
        /// 数据访问上下文
        /// </summary>
        public static MySqlConnection Context
        {
            get
            {
                return new MySqlConnection(_connectionStrings);
            }
        }

        /// <summary>
        /// 数据访问上下文
        /// </summary>
        public static IDbConnection DbConnection
        {
            get
            {
                return new MySqlConnection(_connectionStrings);
            }
        }

        /// <summary>
        /// 释放对象
        /// </summary>
        public void Dispose()
        {
            if (connection != null && connection.State != ConnectionState.Closed)
            {
                connection.Close();
            }
        }
    }
}
